const loginButton = document.querySelector(".loginButton");
const RegisterButton = document.querySelector(".registerButton");
const cutButton = document.querySelector(".cutButton");
var condition = true;
const token = null;
	
loginButton.onclick = function(){
    const error = document.querySelector("#error");
	const link = 'http://localhost:8384/dinosaur/';
	console.log("正在执行")
    const [userName,passWord] = [username.value,password.value]
    var retNum = /^\d*$/;
    if(retNum.test(userName)){
        axios
            .post(link+'login',{
                account:userName,
                password:passWord
            })
            .then(({data}) => {
                if(data.data != null){
                    this.token = data.data;
                    location.replace("index.html?token="+this.token);
                }else{
                    error.innerHTML = data.msg;
                }
            })
            .catch(function (error) { // 请求失败处理
                console.log("错误", error);
            });
    }else{
        error.innerHTML = "账号只能是数字";
    }
}

RegisterButton.onclick = function(){
	const error = document.querySelector("#error");
	const link = 'http://localhost:8384/dinosaur/';
	const [userName1,passWord] = [username.value,password.value]
	var retNum = /^\d*$/;
	axios
		.post(link+'register',{
			userName:userName1,
			password:passWord
		})
		.then(({data}) => {
			console.log()
			if(data.data.token != null){
				this.token = data.data.token;
				alert("分配给您的账号为："+data.data.account+"请妥善保存!")
				location.replace("index.html?token="+this.token);
			}else{
				error.innerHTML = msg;
			}
		})
		.catch(function (error) { // 请求失败处理
			console.log("错误", error);
		});
}

cutButton.onclick = function(){
	const username = document.getElementById("username");
	const login = document.getElementById("login");
	if(condition){
		username.placeholder = "username";
		login.innerHTML = "Register";
		condition = false;
	}else{
		username.placeholder = "account";
		login.innerHTML = "Login";
		condition = true;
	}
	
}